package com.epic.followup.service.wechat.impl;


import com.epic.followup.conf.PatientConfig;
import com.epic.followup.model.app.PatientDiaryModel;
import com.epic.followup.repository.app.PatientDiaryRepository;
import com.epic.followup.service.wechat.WechatPatientDiaryService;
import com.epic.followup.temporary.DealMessageResponse;
import com.epic.followup.temporary.wechat.patient.diary.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

@Service
public class WechatPatientDiaryServiceImlpl implements WechatPatientDiaryService {


    @Autowired
    private PatientDiaryRepository patientDiaryRepository;

    @Override
    public upLoadDiaryImgResponse uploadDiaryImg(MultipartFile file){
        upLoadDiaryImgResponse res=new upLoadDiaryImgResponse();
        if("".equals(file.getOriginalFilename())){
            res.setErrorCode(502);
            res.setErrorMsg("文件名不能为空");
            res.setImgUrl("");
        }
        try {
            String path = PatientConfig.store_diary;//"static/diaryImg";
            //获取文件名
            String fileName = file.getOriginalFilename();
            //创建一个UUID用时间戳表示
            String UUID = new Date().getTime() + "-";
            //组合成新文件名避免有重复的文件名
            String newFileName = UUID + fileName;
            File destFile = new File(new File(path).getAbsolutePath(), newFileName);
            //判断该文件下的上级文件夹是否存在 不存在创建
            if(!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }
            //上传文件
            file.transferTo(destFile);//这一步结束就上传成功了。
            res.setErrorCode(200);
            res.setErrorMsg("上传文件成功");
            res.setImgUrl(PatientConfig.diary_query+newFileName);
        } catch (Exception e) {
            e.printStackTrace();
            res.setErrorCode(502);
            res.setErrorMsg("上传文件失败");
            res.setImgUrl("");
        }
        return res;
    }

    @Override
    public savePatientDiaryResponse saveDiary(String phone, savePatientDiaryRequest saveDiaryRequest){
        savePatientDiaryResponse res=new savePatientDiaryResponse();
        PatientDiaryModel diary=new PatientDiaryModel();
        if(saveDiaryRequest.getId()==-1){//新建日记
            diary.setBrief(saveDiaryRequest.getBrief());
            diary.setHtml(saveDiaryRequest.getHtml());
            diary.setImg(saveDiaryRequest.getImg());
            diary.setMood(saveDiaryRequest.getMood());
            diary.setTime(saveDiaryRequest.getTime());
            diary.setTel(phone);
            patientDiaryRepository.save(diary);
            List<PatientDiaryModel> ou = patientDiaryRepository.findAllByTel(phone);
            PatientDiaryModel lasttm=ou.get(ou.size()-1);
            res.setId(lasttm.getId());
            res.setErrorCode(200);
            res.setErrorMsg("日记插入成功");
            return res;
        }else{//更新日记
            Optional<PatientDiaryModel> ou = patientDiaryRepository.findById(saveDiaryRequest.getId());
            if (!ou.isPresent()){
                res.setId((long) -1);
                res.setErrorCode(504);
                res.setErrorMsg("不存在这篇日记");
                return res;
            }

            diary.setBrief(saveDiaryRequest.getBrief());
            diary.setHtml(saveDiaryRequest.getHtml());
            diary.setImg(saveDiaryRequest.getImg());
            diary.setMood(saveDiaryRequest.getMood());
            diary.setTime(saveDiaryRequest.getTime());
            
            patientDiaryRepository.save(diary);
            res.setId(diary.getId());
            res.setErrorCode(200);
            res.setErrorMsg("日记更新成功");
            return res;
        }
    }

    @Override
    public getPatientDiaryResponse getPatientDiaryById(diaryIdRequest diaryId){
        Long id=diaryId.getId();
        getPatientDiaryResponse res=new getPatientDiaryResponse();
        Optional<PatientDiaryModel> ou = patientDiaryRepository.findById(id);
        if (!ou.isPresent()){
            res.setId((long) -1);
            res.setHtml("");
            res.setMood("");
            res.setTime("");
            res.setErrorCode(504);
            res.setErrorMsg("不存在这篇日记");
            return res;
        }
        PatientDiaryModel diary=ou.get();
        res.setId(diary.getId());
        res.setHtml(diary.getHtml());
        res.setMood(diary.getMood());
        res.setTime(diary.getTime());
        res.setErrorCode(200);
        res.setErrorMsg("查找成功");
        return res;
    }

    @Override
    public DealMessageResponse delPatientDiaryById(diaryIdRequest diaryId){
        Long id=diaryId.getId();
        DealMessageResponse res=new DealMessageResponse();
        Optional<PatientDiaryModel> ou = patientDiaryRepository.findById(id);
        if (!ou.isPresent()){
            res.setErrorCode(504);
            res.setErrorMsg("不存在这篇日记");
            return res;
        }
        patientDiaryRepository.deleteById(id);
        res.setErrorCode(200);
        res.setErrorMsg("删除成功");
        return res;
    }

    @Override
    public getAllPatientDiaryResponse getAllPatientDiary(String phone){
        getAllPatientDiaryResponse res=new getAllPatientDiaryResponse();
        List<PatientDiaryModel> ou = patientDiaryRepository.findAllByTel(phone);
        List<Diary> resDiary=new LinkedList<>();
        for (PatientDiaryModel patientDiaryModel : ou) {
            Diary temp=new Diary();
            temp.setBrief(patientDiaryModel.getBrief());
            temp.setId(patientDiaryModel.getId());
            temp.setImg(patientDiaryModel.getImg());
            temp.setMood(patientDiaryModel.getMood());
            temp.setTime(patientDiaryModel.getTime());
            resDiary.add(temp);
        }
        res.setDiaries(resDiary);
        res.setErrorCode(200);
        res.setErrorMsg("查找成功");
        return res;
    }

    @Override
    public getAllMoodsResponse getAllPatientMoods(String phone) {
        List<MoodList> ou = patientDiaryRepository.findBySqlTel(phone);
        getAllMoodsResponse res = new getAllMoodsResponse();
        res.setMoods(ou);
        res.setErrorCode(200);
        res.setErrorMsg("查找成功");
        return res;
    }
}
